class MouseBoxes < Container
  attr_reader :num_x, :num_y, :box_size, :box_space

  def setup opts = {} 
    @num_x = opts[:num_x] || 8
    @num_y = opts[:num_y] || 6
    @box_size  = opts[:size]  || 0.5
    @box_space = opts[:space] || 0.1

    (0..num_x-1).each do |x|
      (0..num_y-1).each do |y|
        mb = MouseBox.new(@sketch)
        mb.index_x = x
        mb.index_y = y
        self << mb
      end
    end
  end

  def draw_before
    @tx = -(@box_size + @box_space) * (num_x - 0.5) / 2.0
    @ty = -(@box_size + @box_space) * (num_y) / 2.0
    super
  end
end

class MouseBox < Box
  attr_accessor :index_x, :index_y

  def mouse_closeness
    n = 1
    n -= (mouseX/@sketch[:width].to_f - (index_x)/parent.num_x.to_f).abs
    n -= (mouseY/@sketch[:height].to_f - (index_y)/parent.num_y.to_f).abs
    n = 0.05 if n < 0.05
    n
  end

  def draw_before
    speed = 100.0 / mouse_closeness
    scale = 1.0 + mouse_closeness

    size = parent.box_size
    space = parent.box_space

    @rz = pi_pulse(speed)
    @fill = Color.new [0, 1 / (scale), 0.9, 0.8]
    @tx = index_x.to_f * (size + space.to_f) 
    @ty = index_y.to_f * (size + space.to_f) 
    @tz = scale / 2.0
    @sx = @sy = @sz = size * (scale - 0.5)
    super
  end
  
  def inspect
    "#{index_x}, #{index_y}"
  end
end

class MouseBoxesSketch < Sketch3D
  def container
    super do
      block { background 0 }

      object :class => MouseBoxes do
        block { @tz = -4 }
        setup(
          :num_x => 12,
          :num_y => 9,
          :size => 0.4,
          :space => 0.2
        )
      end
    end
  end
end
